﻿(function () {

    layui.use(["form", "layer", "jquery"], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        $(document).ready(function () {

        });

        form.verify({
            password: [/\w{6,12}/, "密码格式不正确！"],
            smscode: [/\d{6}/, "短信验证码格式不正确！"]
        });

        form.on("submit(filter-login)", function (data) {
            //console.log(data);

            var jsonData = {
                Phone: $("#login-phone").val(),
                Pwd: hex_md5($("#login-password").val()).toUpperCase(),
                SMSCode: $("#login-smscode").val()
            };

            var loadingIndex = layer.msg(addStyle("正在登录 ......"), { icon: 16, shade: 0.01, time: false });
            $.ajax("/Account/Login", {
                method: "POST",
                data: jsonData,
                complete: function (jqXHR, textStatus) {
                    layer.close(loadingIndex);
                },
                success: function (data, textStatus, jqXHR) {
                    if (data.Success == true) {
                        layer.msg(addStyle("登录成功！"), {
                            icon: 1, time: 1 * 1000, end: function () {
                                window.location = "/Home/Index";
                            }
                        });
                    } else {
                        layer.msg(addStyle("登录失败: " + data.Msg), { icon: 2, time: 3 * 1000, closeBtn: true });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(addStyle("系统错误，请联系管理员！"), { icon: 2, time: 3 * 1000, closeBtn: true });
                }
            });

            return false;
        });

        function addStyle(str) {
            return '<span style="font-size: 16px;">' + str + '</span>';
        }
    });

})();